package com.demo.study.config;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerExample {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // 使用 Consumer 遍历打印水果名称
        Consumer<String> printFruit = fruit -> System.out.println(fruit);
        forEach(fruits, printFruit);

        // 使用 Consumer 修改水果名称为大写并打印
        Consumer<String> toUpperCase = fruit -> System.out.println(fruit.toUpperCase());
        forEach(fruits, toUpperCase.andThen(printFruit));
    }

    public static <T> void forEach(List<T> list, Consumer<T> consumer) {
        for (T item : list) {
            consumer.accept(item);
        }
    }
}
